package com.sdyd.antivirus.controller;

import com.alibaba.fastjson.JSONObject;
import com.sdyd.antivirus.model.OfficeBuildingInfo;
import com.sdyd.antivirus.service.OfficeBuildingInfoService;
import com.sdyd.antivirus.service.UserInfoService;
import com.sdyd.antivirus.utils.AesEncryptUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("office")
public class OfficeBuildingInfoController {
    Logger logger= LoggerFactory.getLogger(OfficeBuildingInfoController.class);

    @Autowired
    private OfficeBuildingInfoService officeBuildingInfoService;

    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("health")
    public String insertOfficeInfo(OfficeBuildingInfo officeInfo){
        JSONObject obj = new JSONObject();
        obj.put("success",true);
        try {
            //首先验证groupId是否合法
            String groupId = officeInfo.getGroupId();
            Map groupInfo = userInfoService.getGroupInfo(groupId);
            Float heatingDegree = officeInfo.getHeatingDegree();

            String phone = officeInfo.getPhoneNumber();
            AesEncryptUtil.decrypt(phone);
            String cardNumber = officeInfo.getCardNumber();
            AesEncryptUtil.decrypt(cardNumber);

            if(groupInfo == null){
                logger.error("OfficeBuildingInfoController插入用户信息失败：组织id不合法");
                obj.put("success",false);
            }else if(heatingDegree != null && (heatingDegree.floatValue()<35.0 || heatingDegree.floatValue()>42.0) ){
                logger.error("OfficeBuildingInfoController插入用户信息失败：体温范围错误");
                obj.put("success",false);
            }else {
                officeBuildingInfoService.insertOfficeInfo(officeInfo);
            }

        } catch (Exception e) {
            //e.printStackTrace();
            logger.error("OfficeBuildingInfoController插入用户信息失败："+e.getMessage());
            obj.put("success",false);
        }
        return obj.toString();
    }

}
